﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Controls;
using WPFContribDataAccess;
using WPFContribPresenters;
using WPFContrib;
using System.Windows.Automation.Peers;
using System.Windows;
using System.Windows.Input;
using System.Reactive;

namespace WPFContribUnitTests
{
    [TestClass]
    public class PagingControlTests
    {
        [TestMethod]
        public void InitialLoadOfDataIntoPagingControl()
        {
            DataAccessFactory daf = new DataAccessFactory(@"Data Source=test;Version=3;SetDefaults=true", SQLParms.DataAccessType.SQLite);
            ISimpleDataAccess sqliteDataAccess = daf.DataAccess;

            long totalRecords = 0;
            if (sqliteDataAccess != null)
            {
                totalRecords = sqliteDataAccess.Count("SECDATA");
            }

            PageSamplePresenter pgsp = new PageSamplePresenter();

            StackPanel pagingControlStackPanel = pgsp.BuildPagingControl(totalRecords, sqliteDataAccess);

            pgsp.PerformInitialLoadOfData(pagingControlStackPanel, sqliteDataAccess);
            
            SimplePagingControl spgc = pgsp.PagingControl;

            int totalNumberOfPages = spgc.DivideTotalRecordsAlwaysRoundingUp(totalRecords, 250);
            int recordCountInEachPage = 250;
            
            Assert.AreEqual(totalNumberOfPages, spgc.GetTotalNumber());

            Assert.AreEqual(recordCountInEachPage, spgc.MainDataGrid.Items.Count);
        }

        [TestMethod]
        public void PagingControlPageForwardAndBack()
        {
            DataAccessFactory daf = new DataAccessFactory(@"Data Source=test;Version=3;SetDefaults=true", SQLParms.DataAccessType.SQLite);
            ISimpleDataAccess sqliteDataAccess = daf.DataAccess;

            long totalRecords = 0;
            if (sqliteDataAccess != null)
            {
                totalRecords = sqliteDataAccess.Count("SECDATA");
            }

            PageSamplePresenter pagePresenter = new PageSamplePresenter();

            StackPanel pagingControlStackPanel = pagePresenter.BuildPagingControl(totalRecords, sqliteDataAccess);

            pagePresenter.PerformInitialLoadOfData(pagingControlStackPanel, sqliteDataAccess);

            int totalNumberOfPages = pagePresenter.GetTotalNumberOfPages(totalRecords);
            int recordCountInEachPage = 250;

            pagePresenter.FirePageNext(pagingControlStackPanel);
            Assert.AreEqual(recordCountInEachPage, pagePresenter.GetNumberOfRecordsInMainDataGrid());
            Assert.AreEqual(2, pagePresenter.GetStartNumberFromTextBlock());

            pagePresenter.FirePageNext(pagingControlStackPanel);
            Assert.AreEqual(3, pagePresenter.GetStartNumberFromTextBlock());

            pagePresenter.FirePagePrevious(pagingControlStackPanel);
            Assert.AreEqual(recordCountInEachPage, pagePresenter.GetNumberOfRecordsInMainDataGrid());
            Assert.AreEqual(2, pagePresenter.GetStartNumberFromTextBlock());

            pagePresenter.FirePagePrevious(pagingControlStackPanel);
            Assert.AreEqual(1, pagePresenter.GetStartNumberFromTextBlock());
        }
    }
}
